﻿using MKAdminCore.DTO.Web.Common;
using MKAdminCore.IService.Web.Excel;
using MKAdminCore.ToolKit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using MKAdminCore.DTO.Web.TableData.BasicTable;

namespace MKAdminCore.ServiceImpl.Web.Excel
{
    public class ImportService : IImportService
    {
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <returns></returns>
        public async Task<HttpResponseResult> ImportData(DataTable dtResult, UserInfo userInfo)
        {
            var result = new HttpResponseResult();

            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendLine($@"INSERT INTO [dbo].[EmployeeInfo]
                                       ([EmployeeName],[EmployeeSex],[EmployeeAge],[EmployeeHeight]
                                         ,[EmployeeWeight],[PayMoney],[HeadFileName],[ContactPhone]
                                         ,[Education],[AddressDetail],[Hobby],[Motto]
                                         ,CreatorId,[StatusCode],[Status],[EntryTime]
                                         ,[CreateTime],[UpdateTime],[Remark])
                                 VALUES
                                       (@employeeName,@employeeSex,@employeeAge,@employeeHeight
                                       ,55,0,'',@employeePhone
                                       ,@employeeEducation,'','',''
                                       ,{userInfo.OperatorId},0,0,getdate()
                                       ,getdate(),getdate(),'')");

                var employeeInfoList = new List<AddBasicTableParam>();

                for (int i = 0; i < dtResult.Rows.Count; i++)
                {
                    string employeeName = dtResult.Rows[i]["姓名"].ToString();
                    int employeeAge = Convert.ToInt32(dtResult.Rows[i]["年龄"].ToString());
                    string employeeSex = dtResult.Rows[i]["性别"].ToString();
                    int employeeHeight = Convert.ToInt32(dtResult.Rows[i]["身高"].ToString());
                    string education = dtResult.Rows[i]["学历"].ToString();
                    string contactPhone = dtResult.Rows[i]["联系方式"].ToString();

                    int _employeeSex = 0;
                    if (employeeSex.Trim() == "男")
                    {
                        _employeeSex = 1;
                    }
                    else if (employeeSex.Trim() == "女")
                    {
                        _employeeSex = 2;
                    }
                    employeeInfoList.Add(new AddBasicTableParam()
                    {
                        employeeAge = employeeAge,
                        employeeName = employeeName,
                        employeeSex = _employeeSex,
                        employeePhone = contactPhone,
                        employeeEducation = education,
                        employeeHeight = employeeHeight,
                    });
                }

                //util.BulkInserts(dbTable, "EmployeeInfo");
                int queryCount = await conn.ExecuteAsync(sql.ToString(), employeeInfoList);

                result.Msg = "导入成功";
            }

            return result;
        }
    }
}
